package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShortCarService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Api(tags = "C端-购物车接口")
@Slf4j
@RequestMapping("/user/shoppingCart")
public class ShortCarController {

    @Autowired
    private ShortCarService shortCarService;

    @ApiOperation("查看购物车")
    @GetMapping("/list")
    Result<List<ShoppingCart>> list() {
        log.info("查看购物车");
        List<ShoppingCart> list = shortCarService.listByUserId();
        return Result.success(list);
    }

    @ApiOperation("添加购物车")
    @PostMapping("/add")
    Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        shortCarService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }


    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    Result clean() {
        log.info("清空购物车");
        shortCarService.cleanShoppingCartByUserId();
        return Result.success();
    }

    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中一个商品:{}", shoppingCartDTO);
        shortCarService.sub(shoppingCartDTO);
        return Result.success();
    }

}
